package com.s7.ui.arch;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.s7.ui.SUI;

/**
 * Activity基础类
 */
public abstract class ArchActivity extends AppCompatActivity {

    protected String LOG_TAG = this.getClass().getSimpleName();

    /**
     * 当关闭activity时，使activity有新建窗口的能力
     * @return 需要打开的 Activity
     */
    public Intent onLastIntentFinish() {
        return null;
    }

    /**
     * 当关闭activity时，使activity有新建窗口的能力
     * @return 需要打开的 Activity
     */
    public Class<?> onLastActivityFinish() {
        return null;
    }

    @Override
    public void finish() {
        if (!SUI.getInstance().canSwipeBack()) {
            Intent intent = onLastIntentFinish();
            if (intent != null) {
                startActivity(intent);
            }
            if (onLastActivityFinish() != null) {
                startActivity(onLastActivityFinish());
            }
        }
        super.finish();
    }

    /**
     * 获取Bundle对象
     * @return
     */
    public Bundle getBundle() {
        Bundle bundle = getIntent().getBundleExtra("bundle");
        return bundle;
    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
    }

    /**
     * 通过Class跳转界面
     * 关闭时获得该Class页面的返回参数
     **/
    protected void startActivityForResult(Class<?> cls, int sign) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivityForResult(intent, sign);
    }

    /**
     * 含有Bundle通过Class跳转界面
     * 关闭时获得该Class页面的返回参数
     **/
    protected void startActivityForResult(Class<?> cls, int sign, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.putExtra("bundle", bundle);
        startActivityForResult(intent, sign);
    }

    /**
     * 隐藏View对应的软键盘
     * @param v
     */
    public Boolean hideInputMethod(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return false;
    }

    /**
     * 判断当前点击屏幕的地方是否是软键盘
     * @param view
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View view, MotionEvent event) {
        if(view != null && (view instanceof EditText)) {
            int[] leftTop = {0, 0};
            view.getLocationInWindow(leftTop);
            int left = leftTop[0],
                    top = leftTop[1],
                    bottom = top + view.getHeight(),
                    right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                /**
                 * 保留点击EditText的事件
                 */
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}
